package com.zzj.ecology.framework.resolver;

import com.zzj.ecology.commons.domain.Paging;
import com.zzj.ecology.framework.annotation.Pagination;
import com.zzj.ecology.framework.interceptor.PaginationInterceptor;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * @ClassName PaginationHandlerMethodArgumentResolver
 * @Description TODO
 * @Author zhijun zeng at 23:36 2020/5/29
 * @Version 1.0
 **/
@Component
public class PaginationHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterType().isAssignableFrom(Paging.class) && parameter.hasParameterAnnotation(Pagination.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        //获取用户ID
        Object object = nativeWebRequest.getAttribute(PaginationInterceptor.PAGINATION_KEY, RequestAttributes.SCOPE_REQUEST);
        if(object == null){
            return null;
        }
        //业务 操作

        return (Paging)object;
    }
}
